{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='HOME'></a>\n",
    "# CHAPTER 5 Py Boxes: Modules, Packages, and Programs\n",
    "## Python模組、包和程序\n",
    "\n",
    "* [5.1 獨立程式 Standalone Programs](#Standalone)\n",
    "* [5.2 命令列參數 Command-Line Arguments](#Command-Line)\n",
    "* [5.3 模組與匯入 Modules and the import Statement](#Modules_import)\n",
    "* [5.4 打包 Packages](#Packages)\n",
    "* [5.5 標準函數庫 The Python Standard Library](#Standard_Library)\n",
    "* [5.6 獲取其他程式 More Batteries: Get Other Python Code](#Get_Other)\n",
    "\n",
    "\n",
    "程式若要寫得漂亮，乾淨，清楚，則模組化是很重要的一個技術基礎\n",
    "方可把大型的程式拆解能若干小程式，方便管理，維護，重複使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<a id='Standalone'></a>\n",
    "## 5.1 Standalone Programs 獨立程式\n",
    "[回目錄](#HOME)\n",
    "\n",
    "可以把編寫完的py檔於命令提示字元執行\n",
    "\n",
    "```\n",
    ">>> python test1.py\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<a id='Command-Line'></a>\n",
    "## 5.2 Command-Line Arguments 命令列參數\n",
    "[回目錄](#HOME)\n",
    "\n",
    "\n",
    "sys為命令列函數，import後即可在命令列直接輸入參數。\n",
    "\n",
    "```python\n",
    "import sys\n",
    "print('Program arguments:',sys.argv[1])\n",
    "```\n",
    "test2.py tra la \n",
    "```\n",
    ">>> python test2.py\n",
    "Program arguments: ['test2.py']\n",
    ">>> python la\n",
    "Program arguments: ['test2.py', 'tra', 'la', 'la']\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<a id='Modules_import'></a>\n",
    "## 5.3 Modules and the import Statement 模組與匯入\n",
    "[回目錄](#HOME)\n",
    "\n",
    "將程式拆成若干模組(Modules)後，即可使用import匯入，並且可以用as重新取自己想要的名稱\n",
    "\n",
    "程式預設會先搜尋主程式相同路徑的資料夾，若無則搜尋安裝目錄的\\Lib資料夾\n",
    "\n",
    "### import 函式庫( as 別名)\n",
    "為匯入全部的function\n",
    "\n",
    "### from 函式庫 import function( as 別名)\n",
    "為匯入函式庫中的某隻特定function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# weatherman.py\n",
    "# 主程式\n",
    "# P.S.這裡抓不到，因為沒有準備實體函式在資料中\n",
    "\n",
    "import report  #把 report.py 在相同目錄資料夾中，即可對其呼叫匯入\n",
    "description = report.get_description()  #使用report中的get_description函數\n",
    "\n",
    "from get_description import report as get\n",
    "\n",
    "print(\"Today's weather:\", description)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# report.py\n",
    "# 函式庫\n",
    "\n",
    "def get_description():\n",
    "    \"\"\"Return random weather, just like the pros\"\"\"\n",
    "    from random import choice   #匯入標準函式庫 random 中的 choice函數\n",
    "    possibilities = ['rain', 'snow', 'sleet', 'fog', 'sun', 'who knows']\n",
    "    return choice(possibilities)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<a id='Packages'></a>\n",
    "## 5.4 Packages 打包\n",
    "[回目錄](#HOME)\n",
    "\n",
    "__非常非常非常好用的功能!!!!__\n",
    "\n",
    "前述用法是把function拆開再同一層目錄，但是如果函式庫相當的多，在管理上會變得複雜  \n",
    "所以可以將函式庫利用資料夾作為管理，簡易示意圖如下\n",
    "\n",
    "![Alt text](http://i.imgur.com/pCwG1Ze.png)\n",
    "\n",
    "主程式為 __weather.py__  \n",
    "modules為__daily.py__ 與 __weekly.py__  \n",
    "__init.py__檔則為一個空的檔案，目的為使python將sources視為一個函式庫用\n",
    "\n",
    "\n",
    "主程式[__weather.py__]即可使用import匯入sources資料夾中的函示\n",
    "```python\n",
    "#----------------------------------------------------weather.py\n",
    "from sources import daily, weekly\n",
    "\n",
    "print(\"Daily forecast:\", daily.forecast())\n",
    "print(\"Weekly forecast:\")\n",
    "for number, outlook in enumerate(weekly.forecast(), 1):\n",
    "    print(number, outlook)\n",
    "\n",
    "#----------------------------------------------------daily.py：\n",
    "def forecast():\n",
    "    'fake daily forecast'\n",
    "    return 'like yesterday'\n",
    "\n",
    "#----------------------------------------------------weekly.py\n",
    "def forecast():\n",
    "    \"\"\"Fake weekly forecast\"\"\"\n",
    "    return ['snow', 'more snow', 'sleet','freezing rain', 'rain', 'fog', 'hail']\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<a id='Standard_Library'></a>\n",
    "## 5.5 The Python Standard Library 標準函數庫\n",
    "[回目錄](#HOME)\n",
    "\n",
    "介紹一些Python內建的標準函式庫，  \n",
    "PYTHON把一些較不常用的function拆解為標準函式庫，使得python更輕巧\n",
    "\n",
    "詳細介紹請看書本，這邊不多說"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<a id='Get_Other'></a>\n",
    "## 5.6 More Batteries: Get Other Python Code 獲取其他程式\n",
    "[回目錄](#HOME)\n",
    "\n",
    "如果表準函式庫中沒有想到的，可以上到其他網站去尋找，書中介紹了這三個網站\n",
    "\n",
    "* PyPi（ http://pypi.python.org ）\n",
    "* github（ http://github.com/Python ）\n",
    "* readthedocs（ https://readthedocs.org/ )\n",
    "* Python Extension Packages，windows環境超級大補帖 ( http://www.lfd.uci.edu/~gohlke/pythonlibs/ )\n"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
